var express = require('express');
var router = express.Router();

const codeRecord = require("../models/codeRecord")
const demo = require("../models/demo")
const code = require("../models/code")

const formidable = require('formidable');

router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, async function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else {
          if(fields.demoId!==undefined&&fields.demoId!=="") {
              codeRecord.aggregate([
                { $unwind: '$codeRecord_list' },
                { $match: { 
                  'codeRecord_list.content.user': fields.UUID,
                  'codeRecord_list.content.demoId': fields.demoId
              } },
              ], async (err, results) => {
                // console.log("2: " + results.length + JSON.stringify(results[0]) + JSON.stringify(results[1]))
                if (err) {
                  res.json({ 'state': false, 'msg': err })
                } else {
                  let name = ""
                  let return_data = {}
                  await code.findById(fields.demoId, (err, code) => {
                      if(err) {
                          res.json({'state': false, 'msg': err})
                      }else {
                        if(code===null){
                          res.json({'state': false, 'msg': '该案例已不存在！'})
                          return
                        }
                        // console.log(code===null,'oooooooo')
                        name = code.name
                        return_data.html = code.html_content
                        return_data.js = code.js_content
                        return_data.css = code.css_content
                      }
                  })
                  if(results.length===0){
                    res.json({
                      'state': true,
                      'name': name,
                      'code': return_data
                    })
                    return
                  }
                  for(i=results.length-1;i>0; i--){
                    if(results[i].codeRecord_list.type!=="ONLINE_CODING_REALTIME"){
                      return_data.html = results[i].codeRecord_list.content.html
                      return_data.js = results[i].codeRecord_list.content.js
                      return_data.css = results[i].codeRecord_list.content.css
                      return res.json({ 
                        'state': true, 
                        'name': name,
                        'code': return_data
                      })
                    }
                  }
                  res.json({ 
                    'state': true, 
                    'name': name,
                    'code': []
                  })
                }
              })
          } else if(fields.codeDemoId!==undefined&&fields.codeDemoId!==""){
            codeRecord.aggregate([
              { $unwind: '$codeRecord_list' },
              { $match: { 
                'codeRecord_list.content.user': fields.UUID,
                'codeRecord_list.content.codeDemoId': fields.codeDemoId
            } },
            ], async (err, results) => {
              // console.log("2: " + results.length + JSON.stringify(results[0]) + JSON.stringify(results[1]))
              if (err) {
                res.json({ 'state': false, 'msg': err })
              } else {
                let name = ""
                let return_data = {}
                if(results.length===0){
                  res.json({
                    'state': true,
                    'msg': 'no'
                  })
                  return
                }
                for(i=results.length-1;i>0; i--){
                  if(results[i].codeRecord_list.type!=="ONLINE_CODING_REALTIME"){
                    return_data.html = results[i].codeRecord_list.content.html
                    return_data.js = results[i].codeRecord_list.content.js
                    return_data.css = results[i].codeRecord_list.content.css
                    return res.json({ 
                      'state': true, 
                      'code': return_data
                    })
                  }
                }
                res.json({ 
                  'state': true, 
                  'code': []
                })
              }
            })
          }

        }

    });

});

module.exports = router;